import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
// import { stat } from 'fs';


function calculateWinner(squares) {
  const lines = [
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
    [0, 4, 8],
    [2, 4, 6],
    [0, 3, 6],
    [1, 4, 7],
    [2, 5, 8]
  ];
  for (let i = 0; i < lines.length; i++) {
    let [a, b, c] = lines[i];
    if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c] && squares[b] === squares[c]) {
      return squares[a];
    }
  }
  return null;
}


class Square extends React.Component {
  render() {
    return (
      <button
        className="square"
        onClick={() => this.props.onClick()}>
        {this.props.value}
      </button>
    );
  }
}

class Board extends React.Component {


  renderSquare(i) {
    return (
      <Square
        value={this.props.squares[i]}
        onClick={() => this.props.onClick(i)}
      />
    );
  }

  render() {

    return (


      <div>
        {
          Array(3).fill(null).map((itemx, x) => {
            return (
              <div className="board-row" key={x}>
                {
                  Array(3).fill(null).map((itemy, y) => (
                    this.renderSquare(3 * x + y)
                  ))
                }
              </div>)
          })
        }
      </div>
    );
  }
}

class Game extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      history: [{
        squares: Array(9).fill(null),
      }],
      isX: true,
      stepNumber: 0,
      points: [],
      order: true,
    }
  }

  handleClick(i) {
    // slice()是该数组的备份
    const history = this.state.history.slice(0, this.state.stepNumber + 1);
    const current = history[history.length - 1];
    const squares = current.squares.slice();
    let ix = Math.floor(i / 3) + 1;
    let iy = i % 3 + 1;
    let now = ix + "," + iy;
    if (calculateWinner(squares) || squares[i]) {
      return;
    }
    squares[i] = this.state.isX ? "X" : "O";
    this.setState({
      history: history.concat([{
        squares: squares,
      }]),
      stepNumber: history.length,
      isX: !this.state.isX,
      points: this.state.points.concat(now),
    });
  }
  jumpTo(step) {
    this.setState({
      history: this.state.history.slice(0, step + 1),
      stepNumber: step,
      isX: (step % 2) === 0,
      points: this.state.points.slice(0, step),
    });
  }
  changeOrder() {
    this.setState({
      order: !this.state.order,
    });

  }

  render() {
    const history = this.state.history;
    const current = history[this.state.stepNumber];
    const winner = calculateWinner(current.squares);



    const moves = history.map((step, move) => {
      const loc = "( " + this.state.points[move - 1] + " )";
      const desc = move ?
        "Go to move #" + move + loc :
        "Go to game start";
      return (
        <li key={move}>
          <button onClick={() => this.jumpTo(move)}>{desc}</button>
        </li>
      );
    });

    const order = this.state.order ? "降序" : "生序";

    // document.getElementsByClassName("ol").
    let status;
    if (winner) {
      status = "winner: " + winner;
    } else {
      status = "Next player: " + (this.state.isX ? "X" : "O");
    }
    return (
      <div className="game">
        <div className="game-board">
          <Board
            squares={current.squares}
            onClick={(i) => this.handleClick(i)} />
        </div>
        <div className="game-info">
          <div>{status}</div>
          <button onClick={() => this.changeOrder()}>
            {order}
          </button>
          <ul reversed>{this.state.order ? moves : moves.reverse()}</ul>

        </div>
      </div>
    );
  }
}

// ========================================

ReactDOM.render(
  <Game />,
  document.getElementById('root')
);

